// src/views/LoginView.vue
<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <h2>用户登录</h2>
        </div>
      </template>

      <el-form
        :model="formState"
        label-position="top"
        @submit.prevent="onFinish"
      >
        <el-form-item
          label="用户名"
          prop="userName"
          :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]"
        >
          <el-input
            v-model="formState.userName"
            placeholder="请输入用户名"
          >
            <template #prefix>
              <el-icon><UserFilled /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item
          label="密码"
          prop="userPassword"
          :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]"
        >
          <el-input
            v-model="formState.userPassword"
            type="password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item class="form-actions">
          <el-button
            type="primary"
            native-type="submit"
            class="login-button"
          >
            登录
          </el-button>
          <!-- 添加调试按钮 -->
          <el-button
            type="warning"
            class="debug-button"
            @click="debugRedirect"
          >
            调试跳转
          </el-button>
          <div class="link-group">
            <router-link to="/register">
              <el-button link>注册账号</el-button>
            </router-link>
            <router-link
              class="forgot-link"
              to="/forgot-password"
            >
              <el-button link>忘记密码</el-button>
            </router-link>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/user'
import { UserFilled, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { login } from '../api/auth'

const router = useRouter()
const userStore = useUserStore()

const formState = reactive({
  userName: '',
  userPassword: '',
})

const onFinish = async () => {
  try {
    const response = await login(formState)
    if (response.code === "200" && response.ok) {
      userStore.setUser(response.result)
      ElMessage.success('登录成功')
      // 确保使用router.replace而不是router.push
      router.replace('/')
    } else {
      ElMessage.error(response.msg || '登录失败')
    }
  } catch (error) {
    ElMessage.error(error.message || '登录失败')
  }
}

// 添加调试方法
const debugRedirect = () => {
  // 模拟登录状态
  userStore.setUser({ 
    user: { username: '调试用户' },
    token: 'debug-token'
  })
  
  router.replace('/').catch(err => {
    console.error('路由跳转错误:', err)
    ElMessage.error(`跳转失败: ${err.message}`)
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
}

.login-card {
  width: 450px;
  :deep(.el-card__header) {
    background-color: #f5f7fa;
    border-bottom: 1px solid #e4e7ed;
  }
}

.form-actions {
  margin-top: 24px;
}

.link-group {
  display: flex;
  justify-content: space-between;
  margin-top: 12px;
}

.login-button {
  width: 100%;
}

.forgot-link {
  float: right;
}

.debug-button {
  width: 100%;
  margin-top: 12px;
}
</style>